package net.zhengxinyun.unifyApp.Util.httpsUitl;

import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.core.web.JsonBaseController;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class HttpClientService {

    private static final Logger log = LoggerFactory.getLogger(HttpClientService.class);

    private static final int SUCCESS_CODE = 200;

    //get请求；
    public static JSONObject sendGet(String url, List<NameValuePair> nameValuePairs, String protool) throws IOException, KeyManagementException, NoSuchAlgorithmException {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;

        CloseableHttpResponse response = null;

        //创建http对象
        if ("https".equals(protool)) {
            client = new SSLClient();
        } else {
            client = HttpClients.createDefault();
        }

        //创建uriBuilder
        try {
            URIBuilder builder = new URIBuilder(url);
            //设置参数
            builder.addParameters(nameValuePairs);

            //创建httpGET
            HttpGet httpGet = new HttpGet(builder.build());

            // httpGet.setHeader(new BasicHeader("Content-Type", "application/json; charset=utf-8"));

            // httpGet.setHeader(new BasicHeader("Accept","text/plain;charset=utf-8"));

            response = client.execute(httpGet);

            int statueCode = response.getStatusLine().getStatusCode();

            if (SUCCESS_CODE == statueCode) {
                HttpEntity httpEntity = response.getEntity();

                String result = EntityUtils.toString(httpEntity, "UTF-8");

                jsonObject = JSONObject.parseObject(result);

                return jsonObject;
            }

        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            response.close();
            client.close();
        }


        return null;
    }

    //post请求
    public static JSONObject sendPost(String url, String nameValuePairs, String protool) throws IOException {
        JSONObject jsonObject = null;
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            if ("http".equals(protool)) {
                client = HttpClients.createDefault();
            } else {
                client = new SSLClient();
            }

            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(nameValuePairs, ContentType.create("application/json","UTF-8"));
            //设置请求内容

            //设置请求的报文的头部编码
            post.setHeader(new BasicHeader("Content-Type", "application/json; charset=UTF-8"));
            post.addHeader("Content-Type", "application/json; charset=utf-8");
            post.setEntity(entity);
            response = client.execute(post);
            int statuCode = response.getStatusLine().getStatusCode();
            if (SUCCESS_CODE == 200) {
                String result = EntityUtils.toString(response.getEntity(), "UTF-8");
                jsonObject = JSONObject.parseObject(result);
                return jsonObject;
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (KeyManagementException e) {
            e.printStackTrace();
            return null;
        } finally {
            response.close();
            client.close();
        }

        return null;
    }

    //参数类
    public static List<NameValuePair> getParam(Object[] params, Object[] values) {
        boolean flag = params.length > 0 && values.length > 0 && params.length == values.length;
        if (flag) {
            List<NameValuePair> nameValuePairs = new ArrayList<>();
            for (int i = 0; i < params.length; i++) {
                nameValuePairs.add(new BasicNameValuePair(params[i].toString(), values[i].toString()));
            }
            return nameValuePairs;
        } else {
            log.error("参数有误，");
        }

        return null;
    }


}
